package utils.workspace;

import java.io.*;
import java.util.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Juan Carlos García
 */
public class WorkspaceUtils {
    
     /**
     * 
     * This method check the current Workspace depends of the current IDE
     * 
     * @param currentIDE
     * @return String Current workspace of the current IDE
     */
    public static String readWorkspaceFromConfig(String currentIDE,String OS){
        
        String currentWorkspace = "";
     
        
        if("YourEclipse".equals(currentIDE)){
            
            currentWorkspace = readsYourEclipseWorkspace(OS);
            
        }else{
            
            currentWorkspace = readsWorkspaceFromRoute(currentIDE);
            
        }
        
        
        if(currentWorkspace == ""){
            
            JOptionPane.showMessageDialog(null,"Vaya! No hemos podido cargar el Workspace del IDE seleccionado.\n"
                    + "¿Podrías seleccionarlo de forma manual?");
            
            currentWorkspace = WorkspaceSelectorUtils.selectAndReturnWorkspace(JFileChooser.DIRECTORIES_ONLY);
            
        }
        
        currentWorkspace = currentWorkspace.replace('\\','/');
        currentWorkspace = currentWorkspace.replaceAll("C/:","C:/");
 
        return currentWorkspace;
    }
    
    
    /**
     * This method reads the workspace from yourEclipse IDE
     * 
     * @return String Returns the currentWorkspace in yourEclipse IDE
     */
    public static String readsYourEclipseWorkspace(String OS){
        
        String eclipseWorkspace = "";
        
        try{
            
          FileInputStream fstream;
      
          if("Mac OS X".equals(OS)){
              fstream = new FileInputStream("/Applications/yourEclipse/configuration/.settings/org.eclipse.ui.ide.prefs");
          }else{
              fstream = new FileInputStream("C:/yourEclipse/configuration/.settings/org.eclipse.ui.ide.prefs");
          }
          

          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine = "";
          while ((strLine = br.readLine()) != null)   {
              
                StringTokenizer st = new StringTokenizer(strLine, "=");
                
                while(st.hasMoreTokens()) {
                    String key = st.nextToken();
                    
                    if(key.equals("RECENT_WORKSPACES")){
                        eclipseWorkspace = st.nextToken();

                    }
         
                }

          }
       
      }catch(Exception e){
          
          System.out.println(e.getMessage());
      } 
      
        
        return eclipseWorkspace;
    }
    
   /**
     * 
     * @param path The route of the currentIDE
     * @return String Returns the currentWorkspace for the selected item
     */
   public static String readsWorkspaceFromRoute(String path){
       
       String currentWorkspace = "";
       
       try{
      
          FileInputStream fstream = new FileInputStream(path+"/configuration/.settings/org.eclipse.ui.ide.prefs");

          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine = "";
          while ((strLine = br.readLine()) != null)   {
              
                StringTokenizer st = new StringTokenizer(strLine, "=");
                
                while(st.hasMoreTokens()) {
                    String key = st.nextToken();
                    
                    if(key.equals("RECENT_WORKSPACES")){
                        currentWorkspace = st.nextToken();

                    }
         
                }

          }
       
      }catch(Exception e){
          
          System.out.println(e.getMessage());
      } 
       
       return currentWorkspace;
       
   }
    
}

